#ifndef _DETECTOR_H_
#define _DETECTOR_H_

#include "../armor/Armor.h"
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/ml/ml.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include "../assert/Timer/Timer.h"

#define BLUE 0
#define RED 1
#define GRAY_TIME 0.00001
using namespace cv;

#define ADJUST

class Detector
{
private:
    double time_before=0;
    double this_time;
    int src_cols;
    int src_rows;
    Mat binary;
    Mat outline;

    Rect target_roi;                    //框出目标车整个轮廓，专注击打一个目标
    Point roi_move;                     //ROI相对于src的偏移量

    vector<vector<Point>> contours;
    float matchrank[900][900];
    Ptr<ml::SVM> svm;
    
    ////////////////////////////////筛选条件//////////////////////////////////////////////////////////
    int light_angle=15;         //1

    int longshort_ratio=20;      //2

    int differ_centenrx_min=15;  //3
    int differ_centenrx_max=70;
    int differ_centenry_max=13;

    int angle_differ=10;        //4

    int  area_limit=40;         //5
    int  area_ratio=6;

    int color_thresh_red=0;
    int gray_thresh_red=34;
    int color_thresh_blue=45;
    int gray_thresh_blue=169;
    int SVM_thresh=9;
    int num;
    int contour_length_limit=30;       //去除周长太小的轮廓

    Timer clock;                        //计时器，记录掉帧持续时间，应对装甲板灰灭
    Timer clock1;
    Timer clock2;
    Timer clock3,clock4;
public:
    bool islost;
    bool islost_before;                 //上一帧是否是掉帧
    Mat src;                            //读入的原始图片，除划线方便调试外，不对此进行操作
    Mat src_out;                        //对此图片进行操作，一般为ROI
    Mat src_out0;                       //src_out的展示图片，划线输出调试信息都用此图
    Mat number_roi;                     //数字识别用图

    vector<Armor> detect_armor;         //扫描出的装甲板
    Armor target_armor;                 //目标装甲板
    bool enemyColor=RED;
    bool isLock=false;                        //是否处于锁定敌人状态

private:
    void imgProcess(Mat &tempBinary);
    Mat pointProcess(Mat srcImg, int enemyColor, int color_thresh, int gray_thresh);
    int getNumber(vector<Point2f> rect_t);
    int isArmorPattern(Mat &front);
    void getRoi(Point2f center);

public:
    Detector();
    Detector(Mat src0);
    void getSrcImage(Mat& src0);
    void getBinaryImage();
    void getContours();
    void getTarget();
    void adjustParam();
    void getResult(Mat& src0);

};

#endif